package com.jean.com.retrofittest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jean.com.retrofittest.entity.AtlasList;
import com.jean.com.retrofittest.entity.MovieEntity;
import com.jean.com.retrofittest.sercive.MovieService;
import com.jean.com.retrofittest.task.GetAltasTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.click_me_BN)
    Button mclickBtn;
    @Bind(R.id.result_TV)
    TextView result_TV;
    private SubscriberOnNextListener getTopMovieOnNext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.click_me_BN)
    public void onClick(){
        getAtlasListBeta2();
    }

    private void getAtlasListBeta2(){
        GetAltasTask task = new GetAltasTask("1462934556863", "3.4.1.0", "b6a2214", "hz", "867789022783021", "1");
        task.send(new ProgressSubscribe<AtlasList>(this, new SubscriberOnNextListener<AtlasList>(){
            @Override
            public void onNext(AtlasList t0) {
                result_TV.setText(t0.getList().get(0).toString());
            }
        }));
    }

    //beta1
    private void getMovie(){
        String baseUrl = "http://circle2.huizhuang.com/index/diary/";
       // String baseUrl = "https://api.douban.com/v2/movie/";
        //1.构造retrofit请求声明
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();
        //2.构造服务接口
        MovieService movieService = retrofit.create(MovieService.class);
        //new 3.通过rxJava
        movieService.getTopMovie(0,10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MovieEntity>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onNext(MovieEntity movieEntity) {
                        result_TV.setText(movieEntity.toString());
                    }
                });
        // 3.获取请求call
//        Call<MovieEntity> call =  movieService.getTopMovie(0,10);
//        //4.发起请求
//        call.enqueue(new Callback<MovieEntity>() {
//            @Override
//            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
//                result_TV.setText(response.body().toString());
//            }
//
//            @Override
//            public void onFailure(Call<MovieEntity> call, Throwable t) {
//                result_TV.setText(t.toString());
//            }
//        });

    }

//    private void getMoveBeta2(){
//        HttpMethods.getInstance().getMovie(new Subscriber<MovieEntity>() {
//            @Override
//            public void onCompleted() {
//                Toast.makeText(MainActivity.this, "beta2", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//            }
//
//            @Override
//            public void onNext(MovieEntity movieEntity) {
//                result_TV.setText(movieEntity.toString());
//            }
//        }, 0, 10);
//    }

//    //bete1
//    private void getAtlasList(){
//        HttpMethods.getInstance().getAtlasList(new ProgressSubscribe<AtlasList>(this, new SubscriberOnNextListener<AtlasList>() {
//            @Override
//            public void onNext(AtlasList t0) {
//                result_TV.setText(t0.getList().get(0).toString());
//            }
//        }), "1462934556863", "3.4.1.0", "1", "b6a2214", "hz", "867789022783021", "1");
//    }

}

